package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.domain.master.SupportThirdInterface;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.service.mapper.SupportThirdInterfaceService;
import cn.com.connext.scrm.web.rest.vm.SupportThirdInterfaceVM;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 微信公众号三方授权
 */
@RestController
@RequestMapping("/api/supportThirdInterface")
public class SupportThirdInterfaceResource {
    private final Logger logger = LoggerFactory.getLogger(SupportThirdInterfaceResource.class);

    @Autowired
    private SupportThirdInterfaceService supportThirdInterfaceService;


    /**
     * 获取票据
     */
    @ApiOperation(value = "第三方激活二维码添加商品接口", notes = "第三方激活二维码添加商品接口")
    @PostMapping(value = "/addProduct")
    public JsonResult addProduct(@RequestBody SupportThirdInterfaceVM sti ){
        JsonResult jsonResult =new JsonResult();
        try{
            jsonResult =  supportThirdInterfaceService.addProduct(sti.getList());
//            jsonResult.setState(0);
//            jsonResult.setMessage("添加成功");
        }catch (Exception e){
            jsonResult.setState(500);
            jsonResult.setMessage("操作异常!");
            logger.error(e.getMessage());
        }
        return  jsonResult;
    }


    /**
     * 获取票据
     */
    @ApiOperation(value = "查看该二维码是否被商家激活", notes = "查看该二维码是否被商家激活")
    @GetMapping(value = "/supportThirdInterface/isActivate/{uniqueCode}")
    public JsonResult isActivate(@PathVariable("uniqueCode") String uniqueCode){
        JsonResult jsonResult =new JsonResult();
        try{
            Integer count =supportThirdInterfaceService.isActivate(uniqueCode);
            if(count > 0){
                jsonResult.setState(0);
                jsonResult.setMessage("该商品已被激活，可以参与抽奖");
            }else{
                jsonResult.setState(1);
                jsonResult.setMessage("该商品尚未激活，不可参与抽奖");
            }
        }catch (Exception e){
            jsonResult.setState(500);
            jsonResult.setMessage("操作异常");
            logger.error(e.getMessage());
        }
        return  jsonResult;
    }



    /**
     * 获取票据
     */
    @ApiOperation(value = "查看该二维码被使用情况", notes = "查看该二维码被使用情况")
    @PostMapping(value = "/supportThirdInterface/findByUniqueCode")
    public JsonResult findByUniqueCode(@RequestBody SupportThirdInterface stf){
        JsonResult jsonResult =new JsonResult();
        try{
            if(stf !=null){
                if(StringUtils.isNotEmpty(stf.getUniqueCode())){
                    SupportThirdInterface entity =supportThirdInterfaceService.findByUniqueCode(stf.getUniqueCode());
                    jsonResult.setState(0);
                    jsonResult.setMessage("数据入上");
                    jsonResult.setData(entity);
                }else{
                    jsonResult.setState(0);
                    jsonResult.setMessage("参数不能为空!");
                    return jsonResult;
                }
            }else{
                jsonResult.setState(0);
                jsonResult.setMessage("参数不能为空!");
                return jsonResult;
            }
        }catch (Exception e){
            jsonResult.setState(500);
            jsonResult.setMessage("操作异常");
            logger.error(e.getMessage());
        }
        return  jsonResult;
    }

    /**
     * 查询指定日期传输的数据
     */
    @ApiOperation(value = "查询指定日期传输的数据", notes = "查询指定日期传输的数据")
    @PostMapping(value = "/findCountByCreateTime")
    public JsonResult findCountByCreateTime(@RequestBody SupportThirdInterface sti ){
        JsonResult jsonResult =new JsonResult();
        try{
            jsonResult =  supportThirdInterfaceService.findCountByCreateTime(sti.getCreateTime());
//            jsonResult.setState(0);
//            jsonResult.setMessage("添加成功");
        }catch (Exception e){
            jsonResult.setState(500);
            jsonResult.setMessage("操作异常!");
            logger.error(e.getMessage());
        }
        return  jsonResult;
    }
}

